<?php
class TopLoginController  extends Controller {
   
	public function __construct($data) {
        parent::__construct($data);
    }
    
    public function login() {
    	 require_once( '../system/ecjp_m.php' );
    	  $this->_load_php_file("chklogin.php");
    	 
         if(!isset($_SESSION['USER'])){	    	
	    require_once( '../system/cookie.php' );  
	    } 
	    
	    $this->_load_php_file("Class_FUNC.php");
	    $site = Class_FUNC::get_site();
	    if(isset($site['site_keyword']))
	    $this->assign('site_keyword', $site['site_keyword']);
        if(isset($site['site_description']))
	    $this->assign('site_description', $site['site_description']);
	    if(isset($site['site_title']))
	    $this->assign('site_title', 'マイページ ログイン  | '.$site['site_title']);
	    
	    
	     $this->assign('pc_meta', $this->fetch('pc/pc_meta.html'));
    	 $this->assign('pc_head', $this->fetch('pc/pc_head.html'));
    	 $this->assign('pc_foot', $this->fetch('pc/pc_foot.html'));
    	 $this->display('pc/mypage_login.html');
    }
    
    public function  loginout(){
    	    $this->_load_php_file("chklogin.php");
            $this->_load_php_file("cleancookie.php");
    	    if(isset($_SESSION['USER'])){
    	    	  unset($_SESSION['USER']);
    	    }
    	    
    	    if(isset($_SESSION['perURL'])){
    	    	  if(strstr($_SESSION['perURL'],'Mypage')){
    	    	  	  $_SESSION['currentURL'] = $_SESSION['perURL'];
    	    	      header("Location:pc_topLogin_login.html");
    	    	      exit;
    	    	  }else {
    	    	  	    $url = $_SESSION['perURL'];
				        header("Location:$url");
    	    	  }

    	    }
    }
    
    public function chklogin(){
    	
    	$this->_load_php_file("Class_ERROR.php");
    	$this->_load_php_file("Class_DB.php");
    	 require_once('../system/ecjp_m.php');
    	$oerr = new Class_ERROR('','');
    	 $this->_load_php_file("Class_FUNC.php");
	    $site = Class_FUNC::get_site();
	    if(isset($site['site_keyword']))
	    $this->assign('site_keyword', $site['site_keyword']);
        if(isset($site['site_description']))
	    $this->assign('site_description', $site['site_description']);
	    if(isset($site['site_title']))
	    $this->assign('site_title', 'マイページ ログイン  | '.$site['site_title']);
    	
    	if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ 
    	    $forms = $this->p;   	    
    	    $username = $forms['username'];
	        $password = $forms['password'];
	        
	        $errchk =  array( 1 ,5 );   	
    	    $err_mes['username'] = $oerr->check( $username, $errchk );
	        $err_mes['password'] = $oerr->check( $password, $errchk );
	        
    	    $msg_bg =  '<p class="caution">';
	        $msg_ed =  '</p>';
	        if ($err_mes['username']){
		      $err_msg =$msg_bg."IDは".$err_mes['username'].$msg_ed;
	        }
	
	        if ($err_mes['password']){
		      $passmsg = $msg_bg."パスワードは".$err_mes['password'].$msg_ed;
		    if ($err_msg) {
			$err_msg = $err_msg.$passmsg;
		    } else {
			$err_msg = $passmsg;
		    }
	      }
	      	
	      if(  $oerr->clear ){
		     $db = new Class_DB;

		     $sql = sprintf("SELECT * FROM M_USER WHERE login_id = '%s' AND login_pwd = '%s' ",
			 mysql_real_escape_string( $username ),
			 mysql_real_escape_string( $password ));
			 
			 $db->query( $sql );

		     if( $db->num_rows() ){
			 $row = $db->fetch();
			      if($row['state'] == '2'|| $row['state'] == '3') {
				      $err_msg = '<p class="caution">ログインできません。管理者にお問い合わせください。</p>';
				      $err = true;
				      $db->close();				
			       } else {
			       	    $_SESSION['USER']['USER_id']	= $row['id'];
				        $_SESSION['USER']['USER_site_id']	= $row['site_id'];
				        $_SESSION['USER']['USER_login_id']	= $row['login_id'];
				        $_SESSION['USER']['USER_name']	= $row['user_name1'].'  '.$row['user_name2'];
				        $_SESSION['USER']['USER_local_cd']	=  $row['local_cd'];
				        
				        $this->_load_php_file("Class_Cart.php");
				        Class_Cart::setLoginCart($row['id']);
				        
				        
				        //cookie
				        if ($forms['cookie']=='1'){
				        	
				           setcookie("cookie_chk","logined",time()+3600*24);
                           setcookie("cookie_id",$row['id'],time()+3600*24);
                           setcookie("cookie_site_id",$row['site_id'],time()+3600*24);
                           setcookie("cookie_login_id",$row['login_id'],time()+3600*24);
                           setcookie("cookie_name",$row['user_name1'].'  '.$row['user_name2'],time()+3600*24);
                           setcookie("cookie_local_cd",$row['local_cd'],time()+3600*24);
                           
				        }
                        
				        
				        $db->close();
				        $url = $_SESSION['perURL'];
				        header("Location:$url");
				       //echo  $_SERVER["REQUEST_URI"];
				        exit;
			      }
			 }else{
			       $err_msg = '<p class="caution">IDまたはパスワードが間違っています</p>';
			       $err = true;
			       $db->close();
		     }
    	}else{
		$err = true;
	  }
    	$err = true;
    }
     if( isset( $forms ) )  $this->assign('forms', $forms );
     $this->assign('err', $err );
     if( isset( $err_msg ) ) $this->assign('err_msg', $err_msg );
     
      $this->assign('pc_meta', $this->fetch('pc/pc_meta.html'));
     $this->assign('pc_head', $this->fetch('pc/pc_head.html'));
     $this->assign('pc_foot', $this->fetch('pc/pc_foot.html'));
     $this->display('pc/mypage_login.html');
  }
}

?>